import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login.vue' // 导入登录组件
import home from '../components/home.vue' // 导入主页主件
import welcome from '../components/welcome.vue' // 导入欢迎组件
import users from '../components/user/users.vue'
import rights from '../components/power/rights.vue'
import roles from '../components/power/roles.vue'
import Cate from '../components/goods/cate.vue'
import Params from '../components/goods/params.vue'
import List from '../components/goods/list.vue'
import Add from '../components/goods/add.vue'
import Order from '../components/order/order.vue'
import Report from '../components/report/report.vue'
Vue.use(VueRouter)
// 路由规则
const routes = [{
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [{
        path: '/welcome',
        component: welcome
      },
      {
        path: '/users',
        component: users
      },
      {
        path: '/rights',
        component: rights
      },
      {
        path: '/roles',
        component: roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: Add
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }

    ]
  }
]

const router = new VueRouter({
  routes
})
// 为new出来的实例对象,挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  // 这里证明用户想访问有权限的页面
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token,强制跳转到登录也,后期配合axios拦截器
  if (!tokenStr) return next('/login')
  next()
})
export default router